package cn.group.controller;


import cn.group.domain.Classes;
import cn.group.service.ClassService;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassService classService;
    @GetMapping("/showClass")
    public String getClassShow(){return "classInfoIndex";}
    @RequestMapping("/getClassList")
    @ResponseBody
    public Map<String,Object> getClassList(){
        List<Classes> classes=classService.getAll();
        System.out.println(classes);
        Map<String,Object> result=new HashMap<>();
        result.put("code",0);
        result.put("classList",classes);
        result.put("message","获取班级信息成功");
        System.out.println(result);
        return result;
    }
    //分页查询
    @RequestMapping("/getClassListByPage")
    @ResponseBody
    public Map<String,Object> getClassListByPage(Integer currentPage,Integer pageSize){
        PageInfo<Classes> pageInfo=classService.getAll(currentPage,pageSize);
        System.out.println(pageInfo.getList());
        Map<String,Object>result=new HashMap<>();
        result.put("code",0);
        result.put("classList",pageInfo.getList());
        result.put("totalPage",pageInfo.getPages());
        result.put("message","获取用户数据成功");
        return result;
    }
    @RequestMapping("/getOneClass")
    @ResponseBody
    public Map<String, Object> getOneClass(Integer id){
        Map<String, Object> result = new HashMap<>();
        Classes classes = classService.getOneById(id);
        if(classes == null){
            result.put("code", -1);
            result.put("message", "获取数据失败");
        }else{
            result.put("code", 0);
            result.put("classes", classes);
            result.put("message", "获取数据成功");
        }
        return result;
    }
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> edit(@RequestBody Classes classes){
        Map<String, Object> result = new HashMap<>();
        boolean changed;
        changed = classService.changeOne(classes);
        if(!changed){
            result.put("code", -1);
            result.put("message", "编辑数据失败");
        }else{
            result.put("code", 0);
            result.put("message", "编辑数据成功");
        }
        return result;
    }
    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody Classes classes){
        Map<String, Object> result = new HashMap<>();
        boolean changed;
        changed = classService.addOne(classes);
        if(!changed){
            result.put("code", -1);
            result.put("message", "编辑数据失败");
        }else{
            result.put("code", 0);
            result.put("message", "编辑数据成功");
        }
        return result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(Classes classes){
        Map<String,Object> result=new HashMap<>();
        boolean deleted=classService.removeOne(classes);
        if (!deleted){
            result.put("code",-1);
            result.put("message","删除用户数据失败");
        }else {
            result.put("code",0);
            result.put("message","删除用户数据成功");
        }
        return result;
    }

}
